import random
from IC import *
# 精美的界面完成
print("************************************")
print("|           中国工商银行            |")
print("|            V1.0                  |")
print("************************************")
print("|            1、开户               |")
print("|            2、存钱               |")
print("|            3、取钱               |")
print("|            4、转账               |")
print("|            5、查询               |")
print("|            6、再见               |")
print("***********************************")
#bank = {}  # 空字典
# bank={78913791: {'username': 'Frank', 'password': '123456', 'country': '俄罗斯', 'province': '乌克兰', 'street': '基辅', 'door': '001', 'bank_name': '北京市昌平区沙河镇老牛湾车站沙阳路中国工商银行分行街道办支行银行', 'money': 0}}
bank_name = "中国工商银行昌平支行"


# 全局变量
def user_info():
    account = random.randint(10000000, 99999999)
    username = input("请输入您的用户名")
    password = input("请输入您的密码")
    country = input("\t\t请输入您的国家")
    province = input("\t\t请输入您的省份")
    street = input("\t\t请输入您的街道")
    door = input("\t\t请输入您的门牌号")
    x = addbank(account, username, password, country, province, street, door)
    if x == 1:
        print("添加用户成功，以下是您的信息")
        info = '''
        ------------%s-----------
                                    账号：%s
                                    用户名:%s
                                    密码：********
                                    国家：%s
                                    省份：%s
                                    街道：%s
                                    门牌号：%s
                                    余额：%s
        '''
        sql="select money from user where account=%s"
        par=[account]
        money=int(select(sql,par,mode='one')[0])
        print(info % (bank_name, account, username, country, province, street, door, money))
    elif x == 2:
        print("账号重复")
    elif x == 3:
        print("数据库满")
#查找账户
def select_account_(account):
    sql="select account from user where account=%s"
    par=[account]
    return len(select(sql,par))
def count_account():
    sql='select count(*) from user where  account'
    return select(sql,mode="one")[0]
#查找账户
def find_account(account):
    sql ="select password from user where account=%s"
    param =[account]
    account1 = select(sql,param,mode="one")
    if account1 is None:
        return False
    else:
        return True
#查找密码
def find_password(account):
    sql ="select PASSWORD from user where account=%s"
    param=[account]
    password=select(sql,param,mode="one")[0]
    return password
#更新钱
def compara_money(account):
    sql ="select money from user where account=%s"
    param=[account]
    money1=select(sql,param,mode="one")[0]
    return money1
#添加账户
def addbank(account, username, password, country, province, street, door):
    if count_account() >= 100:
        return 3
    elif select_account_(account)==1:
        return 2
    else:
        sql="INSERT INTO USER VALUES(%s,%s,%s,%s,%s,%s,%s,%s,now(),%s)"
        par=[account,username,password,country,province,street,door,0,bank_name]
        insert(sql,par)
        # bank[account] = {
        #     "username": username,
        #     "password": password,
        #     "country": country,
        #     "province": province,
        #     "street": street,
        #     "door": door,
        #     "bank_name": bank_name,
        #     "money": 0
        # }
        return 1


#存钱
def deposit_money():
    ac=input("请输入你的账号")
    ac=int(ac)
    qian=input("请输入你的存储金额")
    qian=int(qian)
    a= find_account(ac)
    if  a:
        sql = "update user set money=money+%s where account=%s"
        param1 = [qian,ac]
        update(sql,param1)
        print("存钱成功")
        return 1
    else:
        return 2
    # while True:
    #     account = input("请输入你的账号")
    #     if account.isdigit():
    #         account = int(account)
    #         break
    #     else:
    #         print("请重新输入")
    # while True:
    #     money1 = input("请输入存储金额")
    #     if money1.isdigit():
    #         money1 = int(money1)
    #         break
    #     else:
    #         print("请重新输入")
    # global bank
    # if account in bank:
    #     bank[account]["money"] = bank[account]["money"] + money1
    #     print("用户", bank[account]["username"], "您好，你目前的余额为", bank[account]["money"])
    # else:
    #     return False

#取钱
def withdraw_money():
    ac = int(input("请输入你的账号"))
    password=input("请输入你的密码")
    qian = int(input("请输入取出金额"))
    a = find_account(ac)
    if a==False:
        return 1
    elif not password == find_password(ac):
        return 2
    elif qian > compara_money(ac):
        return 3
    else:
        sql = "update user set money=money-%s where account=%s"
        param1 = [qian, ac]
        update(sql, param1)
        print("转出成功")
        return 1

    # while True:
    #     account = input("请输入你的账号")
    #     money1 = input("请输入取出金额")
    #     if account.isdigit():
    #         account = int(account)
    #     if money1.isdigit():
    #         money1 = int(money1)
    #         break
    #     else:
    #         print("请重新输入")
    # password = input("请输入你的密码")
    # if account in bank:
    #     if bank[account]["password"] == password:
    #         if bank[account]["money"] < money1:
    #             return 1
    #         else:
    #             bank[account]["money"] = bank[account]["money"] - money1
    #             print("用户", bank[account]["username"], "您好，你目前的余额为", bank[account]["money"])
    #     else:
    #         return 2
    # else:
    #     return 3

#转账
def transfer_money():
    ac = int(input("请输入你的账号"))
    acc = int(input("请输入转入的账号"))
    password = input("请输入密码")
    qian = int(input("请输入转出金额"))
    a = find_account(ac)
    b = find_account(acc)
    if a == False or b==False:
        return 1
    elif not password == find_password(ac):
        return 2
    elif qian > compara_money(ac):
        return 3
    else:
        sql = "update user set money=money-%s where account=%s"
        sql1 = "update user set money=money+%s where account=%s"
        param1 = [qian, ac]
        update(sql, param1)
        param2 = [qian, acc]
        update(sql1, param2)
        print("转出成功")
        print("转入成功")
    # while True:
    #     account = input("请输入你的账号")
    #     account1 = input("请输入转账账号")
    #     money2 = input("请输入转出金额")
    #     if account.isdigit():
    #         account = int(account)
    #     if money2.isdigit():
    #         money2 = int(money2)
    #         break
    #     else:
    #         print("请重新输入")
    # password = input("请输入你的密码")
    # if account in bank:
    #     if bank[account]["password"] == password:
    #         if bank[account]["money"] < money2:
    #             return 1
    #         else:
    #             bank[account]["money"] = bank[account]["money"] - money2
    #             bank[account1]["money"] = bank[account1]["money"] + money2
    #             print("转账成功")
    #     else:
    #         return 2
    # else:
    #     return 3

#查询
def query():
    ac = int(input("请输入你的账号"))
    password = input("请输入你的密码")
    if find_account(ac) :
        if password==find_password(ac):
            sql ="select * from user where account=%s"
            param =[ac]
            print(select(sql, param, mode="one"))
        else:
            return 2
    else:
        return 1


    # global bank
    # while True:
    #     account = input("请输入要查询的账号")
    #     if account.isdigit():
    #         account = int(account)
    #         break
    #     else:
    #         print("请输入正确的账号")
    # password = input("请输入密码")
    # if account in bank:
    #     if bank[account]["password"] == password:
    #         print("账户信息：")
    #         info = '''
    #                ------------%s-----------
    #                    账号：%s
    #                    用户名:%s
    #                    密码：********
    #                    国家：%s
    #                    省份：%s
    #                    街道：%s
    #                    门牌号：%s
    #                    余额：%s
    #                '''
    #         print(info % (bank[account]["username"], account, bank[account]["country"], bank[account]["province"],
    #                       bank[account]["street"], bank[account]["door"], bank[account]['money'],
    #                       bank[account]["bank_name"]))
    #     else:
    #         print("密码错误")
    # else:
    #     print("账户不存在")


while True:
    op = input("请选择业务")
    if op == "1":
        print("开户")
        user_info()
    elif op == "2":
        print("存钱")
        deposit_money()
    elif op == "3":
        print("取钱")
        x = withdraw_money()
        if x == 1:
            print("账号不存在")
        elif x == 2:
            print("密码错误")
        elif x == 3:
            print("余额不足")
        withdraw_money()
    elif op == "4":
        print("转账")
        y = transfer_money()
        if y == 1:
            print("余额不足")
        elif y == 2:
            print("密码错误")
        elif y == 3:
            print("账号不存在")
        transfer_money()
    elif op == "5":
        print("查询账户")
        z=query()
        if z ==1:
            print("账号错误")
        elif z==2:
            print("密码错误")
        query()
    elif op == "6":
        print("欢迎下次使用")
        break
    else:
        print("请正确输入")


